package com.omenkk.juc.ReentrantLock;

import com.omenkk.juc.util.Slepper;

import java.util.concurrent.locks.LockSupport;

///两个线程，先打印1再打印2
public class demo5 {

    //锁
    static final Object obj=new Object();
    //运行标记
    static boolean flag=false;
    public static void main(String[] args) {
//m1()  synchonized版
        m2(); //park unpark版
    }

    private static void m2() {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println(1);
        });
        t1.start();
        Slepper.sleep(1);
        new Thread(()->{
            System.out.println(2);
            LockSupport.unpark(t1);
        }).start();
    }

    static void m1(){
        new Thread(()->{
            synchronized (obj){
                if(!flag){
                    try{
                        obj.wait();
                    }
                    catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
            System.out.println(1);
        }).start();
        Slepper.sleep(1);
        new Thread(()->{
            synchronized (obj){
                System.out.println(2);
                flag=true;
                obj.notify();
            }
        }).start();
    }
}
